package com.example.todo_app;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.example.todo_app.model.ListElement;

import de.htwds.emrolab.mada.activities.MadaArrayAdapter;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class HauptActivity extends Activity {

	private List listOfData = new ArrayList();
	private final int MY_REQUEST_CODE = 1;
	private ListElement listElement;
	private ListView listView;
	private static MadaArrayAdapter adapter = null;

	public static final String LOG_TAG = HauptActivity.class.getName();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_haupt);
		adapter = new MadaArrayAdapter(this);
		listView = (ListView) findViewById(R.id.listView1);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(HauptActivity.this,
						SettingsActivity.class);
				intent.putExtra("ListIndex", arg2);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_haupt, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_Add:
			startSettings();
			break;
		case R.id.menu_Size:
			startTextSize();
			break;
		case R.id.menu_Cate:
			startCategory();
			break;
		case R.id.menu_Prio:
			startPriority();
			break;
		
		}
		return super.onOptionsItemSelected(item);
	}

	public void startSettings() {
		Intent intent = new Intent(this, SettingsActivity.class);
		startActivityForResult(intent, MY_REQUEST_CODE);
		// startActivity(intent);
	}
	
	public void startTextSize(){
		Intent intent = new Intent(this, DetailActivity.class);
		startActivity(intent);
	}
	
	public void startCategory(){
		Intent intent = new Intent(this, CategoryActivity.class);
		startActivity(intent);
	}
	
	public void startPriority(){
		Intent intent = new Intent(this, PriorityActivity.class );
		startActivity(intent);
	}
	

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			List stringData = new ArrayList();
			stringData = data.getStringArrayListExtra("ListObject");
			boolean prio;
			if (stringData.get(2) == "High")
				prio = true;
			else
				prio = false;
			Log.d("TEST", (String) stringData.get(2));
			Log.d("TEST","prio: "+ prio);
//			listElement = new ListElement((CharSequence) stringData.get(0),
//					(CharSequence) stringData.get(1), prio);
			listOfData.add(listElement);
			adapter.add(listElement);
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if (listOfData != null) {
			outState.putSerializable("Test", (Serializable) listOfData);
			super.onSaveInstanceState(outState);
		}
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		/* gespeicherten Wert wieder auslesen */
		this.listOfData = (List) savedInstanceState.getSerializable("Test");
		if (listOfData != null) {
			for (int i = 0; i < listOfData.size(); i++) {
				adapter.add(listOfData.get(i));
			}
			// adapter.add(listOfData);
			listView.setAdapter(adapter);
		}
	}
}
